package com.dps.metrics.metadata;

public interface MetricsMetadata {
    static MetricsMetadata buildFrom(String prefix, MetricsMetadata baseMetric) {
        return buildFrom(prefix, baseMetric, baseMetric.key());
    }

    static MetricsMetadata buildFrom(String prefix, MetricsMetadata baseMetric, String help) {
        return buildMetrics("fusion." + prefix + "." + baseMetric.key(), baseMetric.type(), help);
    }

    static MetricsMetadata buildMetrics(String prefix, final String name, final MetricsMetadata.Type type, final String help) {
        return buildFrom(prefix, new MetricsMetadata() {
            public String key() {
                return name;
            }

            public MetricsMetadata.Type type() {
                return type;
            }

            public String help() {
                return help;
            }
        }, help);
    }

    static MetricsMetadata buildMetrics(final String name, final MetricsMetadata.Type type, final String help) {
        return new MetricsMetadata() {
            public String key() {
                return name;
            }


            public MetricsMetadata.Type type() {
                return type;
            }
        };
    }

    String key();

    default String help() {
        return this.key().replaceAll("\\.", " ").toLowerCase();
    }

    MetricsMetadata.Type type();

    public static enum Type {
        GAUGE,
        TIMER,
        COUNTER,
        SUMMARY;

        private Type() {
        }
    }

    public static enum Metrics implements MetricsMetadata {
        REQUEST_COUNT {
            public String key() {
                return "request.count";
            }

            public String help() {
                return "当前请求数";
            }

            public MetricsMetadata.Type type() {
                return MetricsMetadata.Type.COUNTER;
            }
        },
        RESPONSE_TIME {
            public String key() {
                return "response.time";
            }

            public String help() {
                return "响应时间";
            }

            public MetricsMetadata.Type type() {
                return MetricsMetadata.Type.TIMER;
            }
        },
        IN_PROCESS_COUNT {
            public String key() {
                return "in.process.count";
            }

            public String help() {
                return "当前正在处理的请求";
            }

            public MetricsMetadata.Type type() {
                return MetricsMetadata.Type.GAUGE;
            }
        };

        private Metrics() {
        }
    }

    public static class Tags {
        public static final String CLASS = "class";
        public static final String ERROR = "error";
        public static final String ENDPOINT = "endpoint";
        public static final String HTTP_METHOD = "method";
        public static final String METHOD_INVOKER = "invoker";
        public static final String HTTP_STATUS = "status";
        public static final String BIZ_CODE = "bizCode";
        public static final String EXCEPTION = "exception";
        public static final String X_INFR_FLOWTYPE = "type";

        private Tags() {
        }
    }

    public static class Const {
        public static final String GLOBAL_PREFIX = "fusion";
        public static final String BAGGAGE_PREFIX = "baggage-";

        private Const() {
        }
    }
    
}
